import Layout from '@/views/layout/Layout'
import { reduce } from 'lodash'
// 遍历后台传来的路由字符串，转换为组件对象
export function handleAsyncRoute(parentPath = '', routes) {
  const asyncRoute = reduce(routes, (total, current) => {
    if (!current.ip) {
      current.path =  current.path
    }
    const route = []
    if (current.children) {
      route.push(current)
      if (!current.ip) {
        route.push(...handleAsyncRoute(current.path, current.children))
      }
    }
    else {
      route.push(current)
    }
    return [...total, ...route]
  }, [])
  return asyncRoute
}
export function filterAsyncRouter(asyncRouterMap, parentPath = '') {
  return asyncRouterMap.filter(route => {
    route.path = parentPath + '/' + route.path
    if (route.component) {
      // Layout组件特殊处理
      if (route.component === 'Layout') {
        route.component = Layout
      } else {
          route.component = loadView(route)
      }
    }
    if (route.children != null && route.children && route.children.length) {
      route.children = dynamicRoutes(filterAsyncRouter(route.children, route.path))
    }
    if(route.ip && route.ip.startsWith("//") ){
      route.name =  route.ip
      route.component = () => import('@/components/OutlinkView/index.vue')
    }
    // if (route.name === '参数库管理') {
    //   // route.path = '/project-market-development/parametermgt'
    //   route.component = () => import('@/components/MicroApp.vue')
    // }
    // console.log("route.children",route.children);
    return true
  })
}

export const loadView = view => {
  // 路由懒加载
  // if (ip) { return () => import('@/components/MicroApp.vue') } // 微应用
  // return () => import('@/components/MicroApp.vue')
  const component=view.component
  return () => import(`@/views/${component}`)
}

// 处理路由
export function handleAsyncRoutes(parentPath = '', data) {
  const asyncRoutes = handleAsyncRoute(parentPath, data)
  const routes = []
  forEach(asyncRoutes, route => {
    if (route.ip || route.component !== 'Layout') {
      routes.push({
        ...route,
        name: route.path,
        path: route.ip ? `/${route.path}/:pathMatch(.*)*` : '/' + route.path,
        component: loadView(route),
        children: loadSubMenu(route),
      })
    }
  })
  return routes
}

export function handleMicroApps(data) {
  const microApps = reduce(
    data,
    (total, current) => {
      const route = []
      if (current.children) {
        route.push(current)
        if (!current.ip) {
          route.push(...handleAsyncRoute(current.path, current.children))
        }
      } else {
        route.push(current)
      }
      return [...total, ...route]
    },
    []
  )
  return microApps
}


// 递归路由懒加载
export function dynamicRoutes(routes) {
  
  return traveralRoute(routes, process.env.NODE_ENV === "production" ? process.env.BASE_URL : "/");

  function traveralRoute(routes, parentPath) {
      for (let i = 0, l = routes.length; i < l; i++) {
          const route = routes[i];
          // const component = route.component;
          // route.component = () => import(`@/views/${component}/index.vue`);
          // console.log("子：" + route.path)
          // console.log("父：" + parentPath)

          route.path = removeMoreSlash(parentPath, route.path)
          if (route.redirect != undefined) {
              route.redirect = removeMoreSlash(process.env.NODE_ENV === "production" ? process.env.BASE_URL : "/", route.redirect)
          }
          // console.log(route.path)
          if (route.children != null && route.children && route.children.length) {
              route.children = traveralRoute(route.children, route.path);
          }
      }
      // console.log(routes);
      return routes;
  }
}

function  removeMoreSlash(parentPath ,path) {
  if (path.startsWith("/") && parentPath.endsWith("/")) {
      path = path.substring(1);
  } else if (!path.startsWith("/") && !parentPath.endsWith("/")) {
      path = path + "/";
  }
  return  parentPath + (path || "");
}